// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from './store'
import axios from 'axios'
import Message from 'element-ui'

Vue.config.productionTip = false
Vue.prototype.$message = Message
Vue.prototype.axios = axios
Vue.use(ElementUI)


Vue.prototype.resetSetItem = function (key, newVal) {
  if (key === "login") {
    // 创建一个StorageEvent事件
    var newStorageEvent = document.createEvent('StorageEvent')
    const storage = {
      setItem: function (k, val) {
        sessionStorage.setItem(k, val)
        // 初始化创建的事件
        newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null)
        // 派发对象
        window.dispatchEvent(newStorageEvent)
      }
    }
    return storage.setItem(key, newVal)
  }
}
router.beforeEach(function (to, from, next) {
  if (to.path !== "/Home/Login") {
    //页面是否登录
    if (sessionStorage.getItem("user")) {
      //本地存储中是否有user(uid)数据
      next(); //表示已经登录
    } else {
      //next可以传递一个路由对象作为参数 表示需要跳转到的页面
      next({
        path: "/Home/Login",
      });
    }
  } else {
    //表示不需要登录
    next(); //继续往后走
  }
});


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  Message,
  components: { App },
  template: '<App/>'
})
